﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using TAR4_Lib.General;

namespace TAR4_Lib.Restaurant.Delivery
{
    public class DeliveryModeCollection : List<DeliveryMode>
    {
        #region Static constructors
        public static DeliveryModeCollection GetAllAvailableDeliveryModeForDeliverySite(int deliverySiteID)
		{
		    DeliveryModeCollection retValue = new DeliveryModeCollection();

            using (SqlConnection conn = new SqlConnection(Settings.GetDBConnectionString()))
            {
                using (SqlCommand cmd = new SqlCommand()
                {
                    Connection = conn,
                    CommandText = "[Restaurant].[SP_DeliverySiteDeliveryMode_CL]",
                    CommandType = System.Data.CommandType.StoredProcedure
                })
                {
                    cmd.Parameters.Add(new SqlParameter("@DeliverySiteID", System.Data.SqlDbType.Int) { Value = deliverySiteID });

                    conn.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        try
                        {
                            while (dr.Read())
                            {
                                switch ((DeliveryModeType)Enum.Parse(typeof(DeliveryModeType), (string)dr["DeliveryModeType"]))
                                {
                                    case DeliveryModeType.SelfService:
                                        retValue.Add(new DeliveryModeSelfService((int)dr["IsDeliveryCheckAvailable"]));
                                        break;
                                    case DeliveryModeType.ToTable:
                                        retValue.Add(new DeliveryModeToTable(
                                            (int)dr["IsDeliveryCheckAvailable"],
                                            (bool)dr["IsRemotePrintRequired"],
                                            dr["RemotePrinterName"] == DBNull.Value ? null : (string)dr["RemotePrinterName"]));
                                        break;
                                    case DeliveryModeType.CallByNumber:
                                        retValue.Add(new DeliveryModeCallByNumber(
                                            (int)dr["IsDeliveryCheckAvailable"],
                                            (bool)dr["IsCheckInRequired"],
                                            (bool)dr["IsRemotePrintRequired"],
                                            dr["RemotePrinterName"] == DBNull.Value ? null : (string)dr["RemotePrinterName"]));
                                        break;
                                    default:
                                        throw new ArgumentOutOfRangeException();
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                }
            }

		    return retValue;
		}
        #endregion
    }
}
